<template>
	<view class="detail">
		<scroll-view scroll-y="true" class="scroll-Y">
			<view class="">
				<image :src="datailList.large_img" mode="widthFix" class="detail-image"></image>
			</view>
			<detail-info
			 :datailList="datailList"
			 @changeCount="changeCount"
			 @titleA = "titleA"
			 @titleB = "titleB"
			 @titleC = "titleC"
			 @titleD = "titleD"
			 ></detail-info>
		</scroll-view>
		<detail-tarbar @chooseLike="chooseLike"
					   @addCart="addCart"
					   @payNow="payNow"
					   :allCartCount="allCartCount" 
					   :isLike="isLike"
					   >
		</detail-tarbar>
	</view>
</template>

<script>
	import DetailTarbar from "./childDetail/DetailTarbar.vue"
	import DetailInfo from "./childDetail/DetailInfo.vue"
	
	import {postMall,getAllCartCount} from "network/cart.js"
	import {getLikeMall,postLikeMall,postNotLikeMall} from "network/like.js"
	
	import { formatDate } from "common/utils.js";
	
	export default{
		onLoad(option) {
			this.pid = option.pid
			uni.request({
				url:"http://www.kangliuyong.com:10002/productDetail",
				method:"GET",
				data:{
					appkey:"U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
					pid:option.pid
				},
				success: (res) => {
					if(res.statusCode == 200 ){
						let result = res.data.result[0]
						result.desc = result.desc.split(/\n/)  
						result.arrs = []
						let items = ['cream','milk','sugar','tem']
						items.forEach((e)=>{
							if(!result[e]) return
							let caption = {
								desc:result[`${e}_desc`],
								currentIndex:0,
								arr:[]
							}
							let content = result[e].split("/")
							content.forEach(f=>{
								let team = {
									teamDesc:`${e}_search`,
									title:f
								}
								caption.arr.push(team)
							})
							result.arrs.push(caption)
						})
						 // result.sugars = {
							//  "desc":result.sugar_desc,
							//  "content":result.sugar.split(/\//)
						 // }
						 // result.tems = {
							//  "desc":result.tem_desc,
							//  "content":result.tem.split(/\//)
						 // }
						this.datailList = result
					}
				}
			})
			this._getAllCartCount()
			this._getLikeMall(option.pid)
		},
		components:{
			DetailTarbar,
			DetailInfo,
		},
		data() {
			return {
				pid:null,
				datailList:{},
				type:[],
				count:1,
				isType:{},
				allCartCount:null,
				isLike:null,
			}
		},
		methods:{
			_getAllCartCount(){
				getAllCartCount().then(res=>{
					this.allCartCount = res.data.result
				})
			},
			payNow(){
				this.isType.pid = this.datailList.pid
				this.isType.typeText = this.type.join("/")
				this.isType.count = this.count
				
				if(this.isType.typeText == ""){
					uni.showToast({
					    title: '请选择商品规格',
					    duration: 2000,
						icon:'none'
					})
				}else {
					//数据添加到后台
					postMall(this.isType).then(res=>{
						if(res.data.code == 3000){
							let sid = []
							sid.push(res.data.sid)
							this._getAllCartCount()
							let date = new Date()
							let time = formatDate(date, "yy-MM-dd hh:mm:ss");
							let sids = JSON.stringify(sid)
							uni.navigateTo({
								url:`../shopping/childShopping/submitOrder/SubmitOrder?sids=${sids}&time=${time}`
							})
						}
					})
				}
			},
			addCart(){
				this.isType.pid = this.datailList.pid
				this.isType.typeText = this.type.join("/")
				this.isType.count = this.count
				
				if(this.isType.typeText == ""){
					uni.showToast({
					    title: '请选择商品规格',
					    duration: 2000,
						icon:'none'
					})
				}else {
					//数据添加到后台
					postMall(this.isType).then(res=>{
						if(res.data.code == 3000){
							uni.showToast({
							    title: res.data.msg,
							    duration: 2000,
								icon:'none'
							})
							this._getAllCartCount()
						}
					})
				}
			},
			changeCount(e){
				this.count = e
			},
			titleA(e){this.type[0] = e},
			titleB(e){this.type[1] = e},
			titleC(e){this.type[2] = e},
			titleD(e){this.type[3] = e},
			_getLikeMall(e){
				getLikeMall(e).then(res=>{
					if(res.data.code == 1000){
						if(res.data.result.length !== 0){
							this.isLike = true
						}else{
							this.isLike = false
						} 
					}
				})
			},
			chooseLike(){
				if(this.isLike){
					postNotLikeMall(this.pid)
					this.isLike = false
				}else{
					postLikeMall(this.pid)
					this.isLike = true
				}
			}
		}
	}
</script>

<style scoped>
	.detail{
		position: relative;
		height: 100vh;
	}
	.detail-image{
		width: 100%;
	}
	.scroll-Y{
		position: absolute;
		top: 0;
		bottom: 100rpx;
		left: 0rpx;
		right: 0rpx;
	}
</style>
